package org.cydonia;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.cydonia.config.Configuration;
import org.cydonia.daemon.Daemon;
import org.cydonia.misc.FileUtil;

/**
 *
 * @author franklin
 */
public class Main {

	public static final String PROGRAM_TITLE = "Cydonia Dynamic DNS Client";

	public static final String PROGRAM_VERSION = "0.1";

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		if (!System.getProperties().getProperty("os.name").equalsIgnoreCase("Linux") && !System.getProperties().getProperty("os.name").equalsIgnoreCase("SunOS")) {
			System.out.println("The Cydonia not have support for this OS. Please use a real operating system, Sun Solaris, RedHat Linux, Fedora Linux, CentOS Linux, Slackware Linux or Ubuntu Linux.");
			System.exit(1);
		}

		Main m = new Main();
		m.doInit(args);
	}

	public void doInit(String args[]) {
		List<String> params = getParamList(args);

		if (params.contains("--sample")) {
			try {
				String confExample = "";
				confExample += "#\n";
				confExample += "# /etc/cydonia/cydonia.conf\n";
				confExample += "#\n";
				confExample += "username = your-username\n";
				confExample += "password = your-password\n";
				confExample += "engine = DynDNS\n";
				confExample += "domain = mydomain.homedns.org\n";
				confExample += "updatetime = 10 #minutes\n";
				File dirCydonia = new File("/etc/cydonia");
				if (!dirCydonia.exists()) {
					dirCydonia.mkdir();
				}
				FileUtil.saveStringOnFile("/etc/cydonia/cydonia.conf", confExample);
				System.out.println("Cydonia example configuration is created! See /etc/cydonia/cydonia.conf");
			} catch (Exception ex) {
				System.out.println("Copy not possible. Try run as root.");
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
		} else {

			Configuration config = new Configuration();

			if (params.contains("--help") || params.isEmpty()) {
				System.out.println(PROGRAM_TITLE + " " + PROGRAM_VERSION);
				System.out.println("---------------------------------");
				System.out.println("");
				System.out.println("--start     - Start daemon mode.");
				System.out.println("--stop      - Stop daemon mode.");
				System.out.println("--sysprops  - Show the system properties.");
				System.out.println("--config    - Show configuration.");
				System.out.println("--sample    - Copy sample cydonia.conf to /etc/cydonia");
				System.out.println("");
				System.out.println("Support: fronald@mcfox.com.br");
				System.out.println("");
			}
			if (params.contains("--sysprops")) {
				System.getProperties().list(System.out);
			}
			if (params.contains("--config")) {
				System.out.println("Config file path: /etc/cydonia/cydonia.conf");
				System.out.println("Username: " + config.getValue(Configuration.USERNAME));
				System.out.println("Password: " + config.getValue(Configuration.PASSWORD));
				System.out.println("Domain: " + config.getValue(Configuration.DOMAIN));
				System.out.println("Engine: " + config.getValue(Configuration.ENGINE));
				System.out.println("Updatetime: " + config.getValue(Configuration.UPDATETIME));
			}
			if (params.contains("--start")) {
				Daemon dm = new Daemon(config);
				dm.getMINUTES();
			}
			if (params.contains("--stop")) {
				Daemon dm = new Daemon(config);
				dm.getMINUTES();
			}
		}
	}

	private static List<String> getParamList(String[] args) {
		List<String> rtn = new ArrayList<String>();
		for (int i = 0; i <= args.length - 1; i++) {
			rtn.add(args[i]);
		}
		return rtn;
	}

}
